package com.menchao.myweb1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class servletDemo
 */
@WebServlet("/servletDemo")

public class servletDemo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servletDemo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        System.err.println("我是服务器,通过request.getParameter()获取的参数:");
        StringBuilder paramSB = new StringBuilder();
        Enumeration<?> paramNames=request.getParameterNames();
        while(paramNames.hasMoreElements()){
            String name=(String)paramNames.nextElement();
            String valueNotDecode=request.getParameter(name);
            String valueDecode=new String(valueNotDecode.getBytes("UTF-8"),"UTF-8");
            paramSB.append(name + "==" + valueDecode+"\n");
        }
        System.out.println("我是服务器,通过param读取的参数如下:\n"+paramSB.toString());
        PrintWriter writer = response.getWriter();  
        writer.write("通过param读取的参数如下:\n"+paramSB.toString());  
        writer.flush();  
        writer.close();
        
        
		
		response.setContentType("text/html;charset=UTF-8");
		
		
		         PrintWriter out = response.getWriter();
		         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		         out.println("<HTML>");
		         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		         out.println("  <BODY>");
		         out.print("中国");
		         
		         out.println("  </BODY>");
		         out.println("</HTML>");
		         out.flush();
		         out.close();
		
		System.out.print("打印...");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
